$(document)
		.ready(
				function() {

					var proId;
					var pSize = 8;
					var restype = 0;
					var pageCount = 10;
					var imageArr=new Array();
					var videoArr=new Array();
					
					/**
					 * 初始化图片上传组件
					 */
					function initImageUploader() {
						
						$("#uploader")
								.pluploadQueue(
										{
											// General settings
											runtimes : 'flash,html5,gears,browserplus,silverlight,html4',
											url : "upload.do",
											// unique_names: true,
											chunk_size : '1mb',
											// rename : true,
											dragdrop : true,
											filters : {
												// Maximum file size
												max_file_size : '2048kb', // 最大只能上传100kb的文件
												prevent_duplicates : true,// 不允许队列中存在重复文件
												// Specify what files to browse
												// for
												mime_types : [ {
													title : "Image files",
													extensions : "jpg,gif,png"
												}

												]
											},
											// Resize images on clientside if we
											// can
											resize : {
												width : 200,
												height : 200,
												quality : 90,
												crop : true
											// crop to exact dimensions
											}, // Flash settings
											flash_swf_url : '<%=request.getContextPath()%>/manage/js/plupload/Moxie.swf',
											// Silverlight settings
											silverlight_xap_url : '<%=request.getContextPath()%>/manage/js/plupload/Moxie.xap',
											// 参数
											// multipart_params: {'user':
											// 'Rocky', 'time': '2012-06-12'}
											multipart_params : {
												'resType' : '0',
												'proId' : proId
											}

										});
					}

					/**
					 * 初始化视频上传组件
					 */
					function initVideoUploader() {

						$("#videoUploader")
								.pluploadQueue(
										{
											// General settings
											runtimes : 'flash,html5,gears,browserplus,silverlight,html4',
											url : "upload.do",
											// unique_names: true,
											chunk_size : '1mb',
											// rename : true,
											dragdrop : true,
											filters : {
												// Maximum file size
												max_file_size : '2048kb', // 最大只能上传100kb的文件
												prevent_duplicates : true,// 不允许队列中存在重复文件
												// Specify what files to browse
												// for
												mime_types : [ {
													title : "Video files",
													extensions : "mp4,wma"
												}

												]
											},
											// Resize images on clientside if we
											// can
											resize : {
												width : 200,
												height : 200,
												quality : 90,
												crop : true
											// crop to exact dimensions
											}, // Flash settings
											flash_swf_url : '<%=request.getContextPath()%>/manage/js/plupload/Moxie.swf',
											// Silverlight settings
											silverlight_xap_url : '<%=request.getContextPath()%>/manage/js/plupload/Moxie.xap',
											// 参数
											multipart_params : {
												'resType' : '1',
												'proId' : proId
											}

										});

						$("#clearVideo").delegate($(this), 'click', function() {
							initVideoUploader();
						});

						$("#clearImages").delegate($(this), 'click',
								function() {
									initImageUploader();
								});
					}
					function refresh() {
						window.location.reload();
					}
					$("#resourceDialog").on('hide.bs.modal', function() {
						
						$("#imageTable tr").each(function() {
							$(this).remove();
						});
						
						$("#videoTable tr").each(function() {
							$(this).remove();

					});
					});
					
					$("#resourceDialog").on('shown.bs.modal', function() {
						proId = $("#resourceDialog input[id='proId']").val()
//						$("#imageTable tr").each(function() {
//							$(this).remove();
//						}
//
//						);
//						$("#videoTable tr").each(function() {
//							$(this).remove();

//						});
						initImageUploader();

						initVideoUploader();
						
						loadResource(0);
						
						//initResImgPagination(restype);

					});

					function loadResource(reType)
					{
						var queryUrl = "loadResouce.do";
						$.ajax({
							url : queryUrl,
							dataType : 'json',
							data : {
								id : proId,
							
								resType : reType

							},
							cache : false,
							success : function(data) {
								$("#pageCount").val(data.pageResult.pageCount);
								$("#pageSize").val(data.pageResult.pageSize);
								imageArr = data.resultList;
								alert("-------------imageArr"+imageArr.length);
								initResImgPagination();

							},

							error : function(html) {
								return;
							}
						});
					}
					
					function initResImgPagination() {

						
						imageArr.length
						
						if(imageArr.length%8==0){
							pageCount=imageArr.length/8;
						}else{
							pageCount=parseInt(imageArr.length/8)+1;
						}
						$("#resImgPagination")
								.jqPaginator(
										{
											totalPages : pageCount,
											visiblePages : pageCount,
											currentPage : 1,

											prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
											page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
											next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
											onPageChange : function(num, type) {
											 alert("pageIndex="+num);
												 alert("type="+type);
												$("#pageIndex").val(num);
												
														var rowLen=0;
														if(imageArr.length%4==0){
															rowLen=imageArr.length/4;
														}else{
															rowLen=parseInt(imageArr.length/4)+1;
														}
														
															var idx=0;
															for (k = 0; k < rowLen; k++) {
																alert("k="+k);
																var rowContent = "<tr id='"+k+"'>";
																for (i = 0; i < 4; i++) {
																	
																	if(idx<imageArr.length){
																    imgRes = imageArr[idx];
																	
																	rowContent = rowContent
																			+ "<td style='text-align: center;'>";
																	rowContent = rowContent
																			+ "<dl><img id='"
																			+ idx
																			+ "' alt='' src='"
																			+ imgRes.url
																			+ "' width='100' height='100' /></dl><dl style='text-align: center;'>"
																			+ imgRes.resName
																			+ "</dl></td>";
																	idx++;
																	}
																	
																}
																rowContent = rowContent+ "</tr>";
																
																$("#imageTable").append(rowContent);
				
															}
														

												
											}
										});
					}
				});